﻿
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using ReadBooks.Book.Core;
using ReadBooks.Comm;
using ReadBooks.Manager.Core;
using ReadBooks.Student.Core;
using ReadBooks.Teacher.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace EFRepository
{
  
    public class EFCommContext: DbContext ,ICommRepository
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("EFRepository");
            Console.ReadLine();
        }
        public DbSet<User> User { get; set; }
        public DbSet<Book> Book { get; set; }
        public DbSet<School> School { get; set; }
        public DbSet<Student> Student { get; set; }
        public DbSet<ReadRecords> ReadRecords { get; set; }
        public DbSet<Teacher> Teacher { get; set; }
        public DbSet<ClassInfo> ClassInfo { get; set; }
        public DbSet<TeacherBooks> TeacherBooks { get; set; }
        public DbSet<ClassBooks> ClassBooks { get; set; }
        // protected DbContext context;
        // protected DbSet<T> dbSet;
        //protected bool IsCommit;//是否自动提交
        //protected T entity;
        Action<ModelBuilder> builderHandle;
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            builderHandle?.Invoke(modelBuilder);
            modelBuilder.Entity<User>();
            base.OnModelCreating(modelBuilder);
        }
        public EFCommContext(DbContextOptions<EFCommContext> options):base(options)
        {
           
        }
        public void ConfigureServices(IServiceCollection services)
        {
            var connection = "Filename=./efcoredemo.db";
            services.AddDbContext<EFCommContext>(options => options.UseSqlite(connection));
            // Add framework services.
           
        }
        public EFCommContext(DbContextOptions<EFCommContext> options, Action<ModelBuilder> action) : base(options)
        {
            builderHandle = action;
        }

        /// <summary>
        /// 获取所有实体对象
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> All<T>() where T : class,IEntity
        {
            //context.Set<T>().AsQueryable();
            return Set<T>().AsQueryable();
        }

        /// <summary>
        /// 根据Lamda表达式来查询实体对象
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public IQueryable<T> Where<T>(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda) where T : class, IEntity
        {
            return Set<T>().Where(whereLambda);
        }

       
        /// <summary>
        /// 根据表达式获取实体数量
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public int Count<T>(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda) where T : class, IEntity
        {
            return Set<T>().Where(whereLambda).Count();
        }

      public  int Add<T>(T model, bool IsCommit = true) where T : class, IEntity
        {
            Set<T>().Add(model);
            int i_flag = IsCommit ? SaveChanges() : 0;
            return i_flag;
        }

        /// <summary>
        /// 实体对象修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
     public   int Update<T>(T model, bool IsCommit = true) where T : class, IEntity
        {
            var entry = Entry<T>(model);
            entry.State = EntityState.Modified;
            Set<T>().Attach(model);
            int i_flag = IsCommit ? SaveChanges() : 0;
            return i_flag;
        }

        /// <summary>
        /// 实体对象根据字段修改
        /// </summary>
        /// <param name="model"></param>
        /// <param name="proName"></param>
        /// <returns></returns>
       public int Update<T>(T model, bool IsCommit = true, params string[] proName) where T : class, IEntity
        {
            var entry = Entry<T>(model);
            entry.State = EntityState.Unchanged;
            foreach (string s in proName)
            {
                entry.Property(s).IsModified = true;
            }
            int i_flag = IsCommit ? SaveChanges() : 0;
            return i_flag;
        }

        /// <summary>
        /// 实体对象删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns
      public  int Delete<T>(T model, bool IsCommit = true) where T : class, IEntity
        {
            //var entry = context.Entry<T>(model);
            //entry.State = EntityState.Deleted;
            Set<T>().Attach(model);
            Set<T>().Remove(model);
            int i_flag = IsCommit ? SaveChanges() : 0;
            return i_flag;
        }

        /// <summary>
        /// 删除复核条件的多个实体对象
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
      public  int Delete<T>(Expression<Func<T, bool>> whereLambda, bool IsCommit = true) where T : class, IEntity
        {
            var dbSet = Set<T>();
            var enties = dbSet.Where(whereLambda).ToList();
            foreach (var item in enties)
            {
                dbSet.Remove(item);
            }
            int i_flag = IsCommit ? SaveChanges() : 0;
            return i_flag;
        }

       
    }
}